package com.itheima.demo4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);

        ExecutorService executorService = Executors.newCachedThreadPool();

        Socket accept = serverSocket.accept();

        BufferedInputStream bufferedInputStream = new BufferedInputStream(accept.getInputStream());

        String name = UUID.randomUUID().toString().replace("-","")+".jpg";
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(name));

        byte[] bytes = new byte[8192];
        int len;
        while((len = bufferedInputStream.read(bytes)) != -1){
            bufferedOutputStream.write(bytes,0,len);
        }

        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
        bufferedWriter.write("上传成功");
        bufferedWriter.newLine();
        bufferedWriter.flush();

        accept.shutdownOutput();


        bufferedOutputStream.close();
        accept.close();
    }

}



